package com.example.dilidili_ting.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.MediaRouteButton;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.dilidili_ting.R;
import com.example.dilidili_ting.adapter.HistoryAdapterTing;
import com.example.dilidili_ting.bean.DataBeanTing;
import com.example.dilidili_ting.bean.LikeBeanTing;
import com.example.dilidili_ting.bean.MovieBeanTing;
import com.example.dilidili_ting.utils.OkhttpUtilsTing;
import com.example.dilidili_ting.utils.SharedPreferencesUtilsTing;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class HistoryActivityTing extends AppCompatActivity {

    private RelativeLayout llBarTing;
    private ImageView ivBackTing;
    private TextView tvTitleTing;
    private GridView historyListTing;
    private HistoryAdapterTing historyAdapterTing;
    private List<MovieBeanTing> listMovieTing;
    private RelativeLayout rlScWuTing;
    private TextView tvScWuTing;
    private DataBeanTing<MovieBeanTing> r;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history_ting);
        initView();
        getData();
    }

    //    初始化控件
    @SuppressLint("WrongViewCast")
    private void initView() {
        llBarTing = (RelativeLayout) findViewById(R.id.ll_bar_ting);
        ivBackTing = (ImageView) findViewById(R.id.iv_back_ting);
        tvTitleTing = (TextView) findViewById(R.id.tv_title_ting);
        historyListTing = (GridView) findViewById(R.id.history_list_ting);
        rlScWuTing = (RelativeLayout) findViewById(R.id.rl_sc_wu_ting);
        tvScWuTing = (TextView) findViewById(R.id.tv_sc_wu_ting);
        ivBackTing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HistoryActivityTing.this.finish();
            }
        });
        tvTitleTing.setText("历史记录");
    }

    //    获取播放记录的接口数据
    private void getData(){
        JsonObject jo = new JsonObject();
        jo.addProperty("userId", SharedPreferencesUtilsTing.readLoginUserID(this));
        OkhttpUtilsTing.isPOST("playHistory/getPlayHistoryByUserId", true, jo, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Gson gson = new Gson();
                final DataBeanTing<List<LikeBeanTing>> r = gson.fromJson(res, new TypeToken<DataBeanTing<List<LikeBeanTing>>>(){}.getType());
                listMovieTing = new ArrayList<>();
                HistoryActivityTing.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(r.data.size() == 0){
                            rlScWuTing.setVisibility(View.VISIBLE);
                            historyListTing.setVisibility(View.GONE);
                        }else {
                            rlScWuTing.setVisibility(View.GONE);
                            historyListTing.setVisibility(View.VISIBLE);
                            for (int i=0;i<r.data.size();i++){
                                getMovieId(r.data.get(i).getMovieId(),r.data,i+1);
                            }
                        }
                    }
                });
            }
        });
    }

//    判断是否有记录
    public static void getwu(Integer count){
        if(count==0){
        }
    }


    //    根据id查找电影详情
    private void getMovieId(Integer id, List<LikeBeanTing> data, Integer i){
//        System.out.println("ii"+id);
        JsonObject jo = new JsonObject();
        jo.addProperty("movieId",id);
        OkhttpUtilsTing.isPOST("movie/getMovieById", true, jo, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {}

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String res = response.body().string();
                Gson gson = new Gson();
                r = gson.fromJson(res, new TypeToken<DataBeanTing<MovieBeanTing>>(){}.getType());
                listMovieTing.add(r.data);
                HistoryActivityTing.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
//                        System.out.println("data.size"+data.size());
                        if(i.equals(data.size())){
                            historyAdapterTing = new HistoryAdapterTing(HistoryActivityTing.this,listMovieTing);
//                            MovielistAdapterTing movielistAdapterTing = new MovielistAdapterTing(HistoryActivityTing.this,listMovieTing);
                            historyListTing.setAdapter(historyAdapterTing);
                        }
                    }
                });
            }
        });
    }

}